app.controller('userCouponsCtrl',['$scope','utilService','$stateParams','userCouponsGiveService','toaster','$modal',
    function ($scope,utilService,$stateParams,userCouponsGiveService,toaster,$modal) {
    $scope.couponsVO={};

    $scope.findAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'coupons',
            params:$scope.couponsVO
        }).then(function(result){
            console.log(result.data);
        });
    }

    $scope.findAll();

    $scope.couponsGive=function(id){
        var param = {
            userId : $stateParams.userId,
            couponsId : id
        };
        // query data.
        openModal("确定要赠送该优惠券吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"userCouponsGiveCtrl",
                message:msg,
                data:param
            });
        }
    }


    $scope.delCoupons=function(id){
        var param = {
            couponsId : id
        };
        // query data.
        openModal("确定要删除该优惠券吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"delCouponsCtrl",
                message:msg,
                data:param
            });
        }
    }

}]);

app.controller('delCouponsCtrl', ['$scope', '$modalInstance','$state','userCouponsGiveService','toaster','modalData',
    function($scope, $modalInstance,$state,userCouponsGiveService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.param = {
            couponsId:modalData.data.couponsId
        };
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            userCouponsGiveService.delCoupons($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.findAll();
                        break;
                    case 640:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该优惠劵!");
                        $scope.isDelModal = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


app.controller('userCouponsGiveCtrl', ['$scope', '$modalInstance','$state','userCouponsGiveService','toaster','modalData',
    function($scope, $modalInstance,$state,userCouponsGiveService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.param = {
            userId: modalData.data.userId,
            couponsId:modalData.data.couponsId
        };
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            userCouponsGiveService.couponsGive($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        break;
                    case 721:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "该优惠劵已经领取过了!");
                        $scope.isDelModal = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
